package br.com.informatos.services;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Messenger;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

	private Handler handler = new Handler(){
		@Override
		public void handleMessage(Message message){
			Object path = message.obj;
			if (message.arg1 == RESULT_OK && path != null) {
				Toast.makeText(MainActivity.this,
						getString(R.string.download_sucesso, path.toString()), Toast.LENGTH_LONG).show();
			}else{
				Toast.makeText(MainActivity.this,
						getString(R.string.download_erro, path.toString()), Toast.LENGTH_LONG).show();
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		Button btn_Start = (Button)findViewById(R.id.btn_start);

		btn_Start.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						DownloadService.class);
				Messenger messenger = new Messenger(handler);
				intent.putExtra("messenger", messenger);
				intent.setData(Uri.parse("cursos.html"));
				intent.putExtra("urlPath", "http://k19.com.br/cursos");
				startService(intent);

			}
		});

	}

}
